<?php
require_once 'Zend/Auth/Storage/Interface.php';
 
class Zend_Auth_Storage_Cookie implements Zend_Auth_Storage_Interface
{
    /**
     * Default cookie name
     */
    const COOKIENAME_DEFAULT = 'Zend_Auth_Cookie';
 
    protected $_name = "";
 
    protected $_value = "";
 
    protected $_expire = null;
 
    protected $_path = "/"; 
 
    protected $_domain = "";
 
    protected $_secure = false;
 
    protected $_httponly = false;
 
    public function __construct($option = self::COOKIENAME_DEFAULT) {
        if (is_string($option)) {
            $this->setName($option);
        } else if (is_array($option)) {
            $this->setCookieParams($option);
        } else if ($option instanceof Zend_Config) {
            $this->setCookieParams($option);
        } else {
            $option = (string)$option;
        }
 
    }
 
    public function __get($name) {
        $func = "get".ucfirst($name);
 
        if(method_exists($this, $func)) {
            return $this->$func(); 
        }
 
        return null;
    }
 
    public function __set($name, $value) {
        $func = "set".ucfirst($name);
 
        if(method_exists($this, $func)) {
            $this->$func($value);  
        } else {
            require_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend_Auth_Storage_Exception('Cannot set the this value .');
        }
    }
 
    public function __toString(){
        return "{$this->_name}={$this->_value}";
    }
 
    /**
     * Defined by Zend_Auth_Storage_Interface
     *
     * Returns true if and only if storage is empty
     *
     * @throws Zend_Auth_Storage_Exception If it is impossible to determine whether storage is empty
     * @return boolean
     */
    public function isEmpty() {
        return !isset($_COOKIE[$this->_name]);
    }
 
    /**
     * Defined by Zend_Auth_Storage_Interface
     *
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.
     *
     * @throws Zend_Auth_Storage_Exception If reading contents from storage is impossible
     * @return mixed
     */
    public function read() {
        return unserialize(base64_decode($_COOKIE[$this->_name]));
    }
 
    /**
     * Defined by Zend_Auth_Storage_Interface
     *
     * Writes $contents to storage
     *
     * @param  mixed $contents
     * @throws Zend_Auth_Storage_Exception If writing $contents to storage is impossible
     * @return void
     */
    public function write($contents) {
        if(headers_sent()) {
            require_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend_Auth_Storage_Exception('Cannot write Cookie because headers have already been sent.');
        }
 
        $this->setValue($contents);
 
        $this->_setcookie($this->_name, $this->_value, $this->_expire, $this->_path, $this->_domain, $this->_secure, $this->_httponly);
    }
 
    /**
     * Defined by Zend_Auth_Storage_Interface
     *
     * Clears contents from storage
     *
     * @throws Zend_Auth_Storage_Exception If clearing contents from storage is impossible
     * @return void
     */
    public function clear() {
        $this->_setcookie($this->_name, "", time()-3600, $this->_path, $this->_domain, $this->_secure, $this->_httponly);
    }
 
    public function setCookieParams($params) {
        if ($params instanceof Zend_Config) {
            $params = $params->toArray();
        } elseif (!is_array($params)) {
            require_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend_Auth_Storage_Exception('setCookieParams expects either an array or a Zend_Config object .');
        }
 
        foreach($params as $key => $value) {
            $method = 'set' . ucfirst($key);
            if(method_exists($this, $method)) {
                $this->$method($value);
            }
        }
    }
 
    public function setName($name) {
       if (!$name = (string)$name) {
            require_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend_Auth_Storage_Exception('Cookies must have a name');
        }
 
        if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
            require_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend_Auth_Storage_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
        }
 
        $this->_name = $name;
    }
 
    public function getName() {
        return $this->_name;
    }
 
    public function setValue($value) {
        if (is_bool($value)) {
            $value = ($value) ? "1" : "0";
        } else if (!is_string($value)) {
            $value = base64_encode(serialize($value));
        }   
        $this->_value = $value;
    }
 
    public function getValue() {
        return unserialize(base64_decode($this->_value));
    }
 
    public function setExpire($expire) {
        if(!is_numeric($expire) || $expire < 0) {
            require_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend_Auth_Storage_Exception('You must provide a cookie expire param grater than or equal 0 .');
        }
 
        $this->_expire = (int)$expire;
    }
 
    public function getExpiryTime() {
        return $this->_expire;
    }
    protected function getExpire() {
        return $this->getExpiryTime();
    }
 
    public function isExpired($now = null) {
        if ($now === null) $now = time();
        if (is_int($this->_expires) && $this->_expires < $now) {
            return true;
        } else {
            return false;
        }
    }
 
    public function setPath($path) {
        $this->_path = $path;
    }
 
    public function getPath() {
        return $this->_path;
    }
 
    public function setDomain($domain) {
        $this->_domain = $domain;
    }
 
    public function getDomain() {
        return $this->_domain;
    }
 
    public function setSecure($secure) {
        $this->_secure = ($secure) ? true : false;
    }
 
    protected function getSecure() {
        return $this->isSecure();
    }
 
    public function isSecure() {
        return $this->_secure;
    }
 
    public function setHttponly($httponly) {
        if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
            $this->_httponly = ($httponly) ? true : false;
        }
    }
 
    protected function getHttponly() {
        if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
            return $this->isHttponly();
        }
    }
 
    public function isHttponly() {
        if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
            return $this->_httponly;
        }
    }
 
    protected final function _setcookie($name, $value, $expire, $path, $domain, $secure, $httponly){
        if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
           setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
        } else {
            setcookie($name, $value, $expire, $path, $domain, $secure);
        }
    }
}	
?>